package com.ruoyi.prediction.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.SnowflakeIdUtils;
import com.ruoyi.control.water.domain.Jingxia;
import com.ruoyi.control.water.mapper.JingxiaMapper;
import com.ruoyi.prediction.dto.PredictionDto;
import com.ruoyi.pressure.domain.KyLaneDblc;
import com.ruoyi.pressure.domain.KyLaneDblcReal;
import com.ruoyi.pressure.domain.KyLaneMgyl;
import com.ruoyi.pressure.domain.KyLaneMgylReal;
import com.ruoyi.pressure.mapper.KyLaneDblcMapper;
import com.ruoyi.pressure.mapper.KyLaneDblcRealMapper;
import com.ruoyi.pressure.mapper.KyLaneMgylMapper;
import com.ruoyi.pressure.mapper.KyLaneMgylRealMapper;
import com.ruoyi.safety.domain.SafetyPointRealTime;
import com.ruoyi.safety.mapper.SafetyPointRealTimeMapper;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.tube.domain.Sgcdss;
import com.ruoyi.tube.mapper.SgcdssMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.prediction.mapper.PredictionMapper;
import com.ruoyi.prediction.domain.Prediction;
import com.ruoyi.prediction.service.IPredictionService;

/**
 * 预测数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-18
 */
@Service
public class PredictionServiceImpl implements IPredictionService 
{
    @Autowired
    private PredictionMapper predictionMapper;

    @Autowired
    private SysDictDataMapper dictDataMapper;

    @Autowired
    private SgcdssMapper sgcdssMapper;

    @Autowired
    private JingxiaMapper jingxiaMapper;

    @Autowired
    private SafetyPointRealTimeMapper safetyPointRealTimeMapper;

    @Autowired
    private KyLaneDblcRealMapper kyLaneDblcRealMapper;

    @Autowired
    private KyLaneMgylRealMapper kyLaneMgylRealMapper;
    /**
     * 查询预测数据
     * 
     * @param predictionId 预测数据主键
     * @return 预测数据
     */
    @Override
    public Prediction selectPredictionByPredictionId(String predictionId)
    {
        return predictionMapper.selectPredictionByPredictionId(predictionId);
    }

    /**
     * 查询预测数据列表
     * 
     * @param prediction 预测数据
     * @return 预测数据
     */
    @Override
    public List<Prediction> selectPredictionList(Prediction prediction)
    {
        return predictionMapper.selectPredictionList(prediction);
    }

    /**
     * 新增预测数据
     * 
     * @param prediction 预测数据
     * @return 结果
     */
    @Override
    public int insertPrediction(Prediction prediction)
    {
        return predictionMapper.insertPrediction(prediction);
    }

    /**
     * 修改预测数据
     * 
     * @param prediction 预测数据
     * @return 结果
     */
    @Override
    public int updatePrediction(Prediction prediction)
    {
        return predictionMapper.updatePrediction(prediction);
    }

    /**
     * 批量删除预测数据
     * 
     * @param predictionIds 需要删除的预测数据主键
     * @return 结果
     */
    @Override
    public int deletePredictionByPredictionIds(String[] predictionIds)
    {
        return predictionMapper.deletePredictionByPredictionIds(predictionIds);
    }

    /**
     * 删除预测数据信息
     * 
     * @param predictionId 预测数据主键
     * @return 结果
     */
    @Override
    public int deletePredictionByPredictionId(String predictionId)
    {
        return predictionMapper.deletePredictionByPredictionId(predictionId);
    }

    @Override
    public void addPrediction() {
        List<SysDictData> sensorTypes = dictDataMapper.selectDictDataByType("sensor_type");
        for (SysDictData sensorType : sensorTypes) {
            Sgcdss sgcdss = sgcdssMapper.getDataBySensorType(sensorType.getDictLabel());
            if (sgcdss != null){
                Prediction prediction = new Prediction();
                prediction.setPredictionId(String.valueOf(new SnowflakeIdUtils(5,8).nextId()));
                prediction.setPredictionTime(new Date());
                prediction.setSensorType(sensorType.getDictLabel());
                prediction.setSensorLocation(sgcdss.getPointArea());
                prediction.setSensorValue(String.valueOf(sgcdss.getPointValue()));
                predictionMapper.insertPrediction(prediction);
            }
            Jingxia jingxia = jingxiaMapper.getDataBySensorType(sensorType.getDictLabel());
            if (jingxia != null){
                Prediction prediction = new Prediction();
                prediction.setPredictionId(String.valueOf(new SnowflakeIdUtils(5,8).nextId()));
                prediction.setPredictionTime(new Date());
                prediction.setSensorType(sensorType.getDictLabel());
                prediction.setSensorLocation(jingxia.getAddress());
                prediction.setSensorValue(jingxia.getValueState());
                predictionMapper.insertPrediction(prediction);
            }
            KyLaneDblcReal kyLaneDblcReal = kyLaneDblcRealMapper.getDataBySensorType(sensorType.getDictLabel());
            if (kyLaneDblcReal != null){
                Prediction prediction = new Prediction();
                prediction.setPredictionId(String.valueOf(new SnowflakeIdUtils(5,8).nextId()));
                prediction.setPredictionTime(new Date());
                prediction.setSensorType(sensorType.getDictLabel());
                prediction.setSensorLocation(kyLaneDblcReal.getSensorLocation());
                prediction.setSensorValue(String.valueOf(kyLaneDblcReal.getQjdMonitorValue()));
                predictionMapper.insertPrediction(prediction);
            }
            KyLaneMgylReal kyLaneMgylReal = kyLaneMgylRealMapper.getDataBySensorType(sensorType.getDictLabel());
            if (kyLaneMgylReal != null){
                Prediction prediction = new Prediction();
                prediction.setPredictionId(String.valueOf(new SnowflakeIdUtils(5,8).nextId()));
                prediction.setPredictionTime(new Date());
                prediction.setSensorType(sensorType.getDictLabel());
                prediction.setSensorLocation(kyLaneMgylReal.getSensorLocation());
                prediction.setSensorValue(String.valueOf(kyLaneMgylReal.getMonitorValue()));
                predictionMapper.insertPrediction(prediction);
            }
            SafetyPointRealTime safetyPointRealTime = safetyPointRealTimeMapper.getDataBySensorType(sensorType.getDictLabel());
            if (safetyPointRealTime != null){
                Prediction prediction = new Prediction();
                prediction.setPredictionId(String.valueOf(new SnowflakeIdUtils(5,8).nextId()));
                prediction.setPredictionTime(new Date());
                prediction.setSensorType(sensorType.getDictLabel());
                prediction.setSensorLocation(safetyPointRealTime.getPointInstallLocation());
                prediction.setSensorValue(safetyPointRealTime.getPointValue());
                predictionMapper.insertPrediction(prediction);
            }
        }
    }

    @Override
    public PredictionDto getData(String sensorType) {
        Calendar calendar = Calendar.getInstance();
        PredictionDto predictionDto = new PredictionDto();
        List<Prediction> predictionList = predictionMapper.selectPredictionBySensorType(sensorType);
        List<String> dateList = new ArrayList<>();
        List<Double> nowData = new ArrayList<>();
        List<Double> nowData1 = new ArrayList<>();
        List<Double> predictionData = new ArrayList<>();
        predictionList.stream().map(prediction -> {
            calendar.setTime(prediction.getPredictionTime());
            dateList.add(String.valueOf(calendar.get(Calendar.HOUR_OF_DAY)));
            nowData.add(Double.valueOf(prediction.getSensorValue()));
            predictionData.add(null);
            return prediction;
        }).collect(Collectors.toList());
        List<Double> temp = new ArrayList<>();
        temp = nowData;
        for (int i = predictionList.size(); i < predictionList.size() + predictionList.size(); i++) {
            double sum = 0.0;
            for (int j = 0; j < i; j++) {
                sum += temp.get(j);
            }
            temp.add(sum / i);
            predictionData.add(sum / i);
        }
        predictionList.stream().map(prediction -> {
            calendar.setTime(prediction.getPredictionTime());
            dateList.add(String.valueOf(calendar.get(Calendar.HOUR_OF_DAY)));
            nowData1.add(Double.valueOf(prediction.getSensorValue()));
            return prediction;
        }).collect(Collectors.toList());
        predictionDto.setDateList(dateList);
        predictionDto.setNowData(nowData1);
        predictionDto.setPredictionData(predictionData);
        return predictionDto;
    }
}
